package com.hx.jeeadminbase.utils;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
 * 类名称：Tools 类描述：暂无 创建人：XXM 创建时间：2014-3-22 下午7:36:10 修改人：XXM
 * 修改时间：2014-3-22 下午7:36:10 修改备注：
 * @version
 */
public class Tools {

	// 操作系统文件路径分隔符
	public static String FS = System.getProperties().getProperty("file.separator");

	/**
	 * 读取Properties配置文件内容
	 * @param filePath
	 * @return Properties
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static Properties readProperties(String filePath) throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		properties.load(new FileInputStream(new File(filePath)));
		return properties;
	}

	/**
	 * 写key-value到properties文件 相同的key会被覆盖 追加不同的key-value
	 * @param key 键
	 * @param value 值
	 * @param filePath 文件路径
	 * @param comment key-value的注释
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void writeProperties(String key, String value, String comment, String filePath) throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		File file = new File(filePath);
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);
			properties.load(fis);
			fis.close();
		}
		properties.setProperty(key, value);
		properties.store(new FileOutputStream(new File(filePath)), comment);
	}

	/**
	 * 获取资源配置文件的值
	 * @param propertyFileName
	 * @param key
	 * @return
	 */
	public static String getPropertyValue(String propertyFileName, String key) {
		Properties props = new Properties();
		InputStream is = null;
		String value = "";
		try {
			is = Tools.class.getResourceAsStream("/" + propertyFileName + ".properties");
			if (is != null) {
				props.load(new InputStreamReader(is, "UTF-8"));
				value = props.getProperty(key);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	/**
	 * 批量读取配置文件参数值
	 * @param propertyFileName
	 * @param keys
	 * @return
	 */
	public static Map<String, String> getPropertyValues(String propertyFileName, String... keys) {
		Map<String, String> rtn = new LinkedHashMap<String, String>();
		Properties props = new Properties();
		InputStream is = null;
		try {
			is = Tools.class.getResourceAsStream("/" + propertyFileName + ".properties");
			if (is != null) {
				props.load(new InputStreamReader(is, "UTF-8"));
				if (keys != null && keys.length > 0) {
					for (String key : keys) {
						rtn.put(key, props.getProperty(key));
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return rtn;
	}

	/**
	 * 更新properties文件的键值对 如果该主键已经存在，更新该主键的值； 如果该主键不存在，则插件一对键值。
	 * @param keyname 键名
	 * @param keyvalue 键值
	 */
	public static void updateProperties(String propertyFilePath, String keyname, String keyvalue) {
		InputStream is = null;
		OutputStream fos = null;
		try {
			Properties props = new Properties();
			// String path=Tools.class.getResource("/").getPath();//得到路径
			// path=URLDecoder.decode(path,"utf-8");//关键啊 ！
			// String ppath = path+propertyFileName+".properties";
			//String fpath = System.getProperty("user.dir") + FS + propertyFileName + ".properties";
			props.load(new FileInputStream(propertyFilePath));
			// 调用 Hashtable 的方法 put，使用 getProperty 方法提供并行性。
			// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
			fos = new FileOutputStream(propertyFilePath);
			props.setProperty(keyname, keyvalue);
			// 以适合使用 load 方法加载到 Properties 表中的格式，
			// 将此 Properties 表中的属性列表（键和元素对）写入输出流
			props.store(fos, "Update '" + keyname + "' value");
		} catch (IOException e) {
			System.err.println("属性文件更新错误");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 获取本机IP
	 * @return
	 */
	public static String getHostIp() {
		InetAddress addr;
		String ip = "";
		try {
			addr = InetAddress.getLocalHost();
			ip = addr.getHostAddress().toString();// 获得本机IP
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}

	/**
	 * 产生指定范围的随机数
	 * @param min
	 * @param max
	 * @return
	 */
	public static int makeRandomNo(final int min, final int max) {
		Random rand;
		rand = new Random();
		int tmp = Math.abs(rand.nextInt());
		return tmp % (max - min + 1) + min;
	}

	public static void main(String[] args) {
		int a = makeRandomNo(100000, 999999);
		System.out.println(a);
	}
}
